﻿package com.mdp.controllers 
{
	import org.papervision3d.core.proto.CameraObject3D;
	import gs.TweenLite;
	import gs.easing.Elastic;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	import com.mdp.input.MainInput;
	
	/**
	 * ...
	 * @author ...
	 */
	public class MovementController 
	{
		
		static protected var _cam:CameraObject3D;
		
	  static public var myVectors:Array;
	  static public var myOffsets:Array;
	  static public var stanVecs:Array;
	  static public var startVecs:Array;
		
		//movement
		static public var moveMode:Number = 1;
	   
	   //unit dist
	   static public var unitDist:Number = 166;
	  static public var subDist:Number;
		
	  static public function init(cam:CameraObject3D):void
	  {
			_cam = cam;
			
			startVecs = new Array( _cam.x, _cam.y, _cam.z );
			
			//calc 'b'
		  subDist = Math.sqrt( unitDist*unitDist - ((unitDist/2)*(unitDist/2)) );
		  
		  //setup vectors
		  myVectors = new Array(); 
		  myVectors[0] = new Array( 0, 0, 0 );
		  myVectors[1] = new Array( 0, 0, 0 );
		  myVectors[2] = new Array( 0, 0, 0 );
		  myVectors[3] = new Array( 0, 0, 0 );
		  myVectors[4] = new Array( 0, 0, 0 );
		  myVectors[5] = new Array( 0, 0, 0 );
		  
		  //standard vecs
		  stanVecs = new Array();

		  //START movement vectors
		  stanVecs[5] = new Array( 87.5, 0, -151.2 );
		  stanVecs[1] = new Array( -175, 0, 0 );
		  stanVecs[3] = new Array( 87.5, 0, 151.2 );
		  stanVecs[4] = new Array( 175, 0, 0 );
		  stanVecs[2] = new Array( -87.5, 0, 151.2 ); 
		  stanVecs[0] = new Array( -87.5, 0, -151.2 );
		  //END MOVEMENT VECTORS
		  
		  //offsets
		  myOffsets = new Array();
		  myOffsets[0] = 0;
		  myOffsets[1] = 0;
		  myOffsets[2] = 0;
		  myOffsets[3] = 0;
		  myOffsets[4] = 0;
		  myOffsets[5] = 0;
		}
		
		static public function handleMovement(e:KeyboardEvent):void {
			var rotY:Number = ( _cam.rotationY * Math.PI ) / 180;
			 //hanlde carosel
			 switch( e.keyCode) {
				case( 82 ):
					_cam.zoom += 3;
					break;
				case( 70 ):
					_cam.zoom -= 3;
					break;
				case ( 83 ):
					
					break;
				case( 65 ): 
					
					break;
				case( 68 ):
					
					break;
				default:
					break;
				 
			 }
			
			
			//handle different move modes
			switch( moveMode ) {
				case ( 0 ):
				
					//"debug" trig-based movement code
					switch( e.keyCode ) {
						
						case ( Keyboard.UP ):
							TweenLite.to(_cam, .5, { 
										 x:_cam.x + ( Math.sin( rotY ) * ( 2* subDist ) ), 
										 z:_cam.z + ( Math.cos( rotY ) * ( 2* subDist ) )
										 });
							break;
						case ( Keyboard.RIGHT ):
							TweenLite.to(_cam, .5, { 
										 //x: default_camera.x + ( Math.sin( rotY + ((45*Math.PI)/180) ) * 60 ), 
										 //z: default_camera.z + ( Math.cos( rotY + ((45*Math.PI)/180) ) * 60 ),
										 rotationY: _cam.rotationY + 60
										 });
							break;
						case ( Keyboard.LEFT ):
							TweenLite.to(_cam, .5, { 
										 //x: default_camera.x + ( Math.sin( rotY - ((45*Math.PI)/180) ) * 60 ), 
										 //z: default_camera.z + ( Math.cos( rotY - ((45*Math.PI)/180) ) * 60 ),
										 rotationY: _cam.rotationY - 60
										 });
							break;
						case ( Keyboard.DOWN ):
							TweenLite.to(_cam, .5, { 
										 x:_cam.x - ( Math.sin( rotY ) * ( 2 * subDist ) ), 
										 z:_cam.z - ( Math.cos( rotY ) * ( 2 * subDist ) )
										 });
							break;
						default:
							//do no thing
							break;
					}
					//break from case 0
					break;
				case ( 1 ):
					//"standard" vector move code
					doStandard( e );
					//break from case 1
					break;
				case( 2 ):
					//break from case 2
					break;
				default:
					break;
			}
		}
		
		static private function sumVectors():void {
		
			var vX:Number = 0, vY:Number = 0, vZ:Number = 0;
			
			for( var i:String in myVectors ) {
				vX += myVectors[i][0];
				vY += myVectors[i][1];
				vZ += myVectors[i][2];
			}
			
			TweenLite.to(_cam, .5, { z: vZ + startVecs[2], x: vX + startVecs[0], onComplete: MainInput.enableInput  });
			//default_camera.x = vX + startVecs[0];
			//default_camera.y = vY;
			//default_camera.z = vZ + startVecs[2];
			
			//TweenLite.to(default_camera, .1, { z: vZ + startVecs[2], x: vX + startVecs[0]  });
			//trace( "Rotation:" + default_camera.rotationY);
	   }
	   
		
	   static private function doStandard( e:KeyboardEvent ):void {

		   var rotationTest:Number = Math.tan( Math.abs( _cam.rotationY ) * ( Math.PI / 180 ) ) * 10;
		   
		   switch( e.keyCode ) {
				case( Keyboard.UP ):
					switch( Math.abs( Math.round(_cam.rotationY)) ) {
						case(30):
							myOffsets[0] --;
							myVectors[0][0] = stanVecs[0][0] * myOffsets[0];
							myVectors[0][1] = stanVecs[0][1] * myOffsets[0];
							myVectors[0][2] = stanVecs[0][2] * myOffsets[0];
							break;
						case(90):
							myOffsets[1] --;
							myVectors[1][0] = stanVecs[1][0] * myOffsets[1];
							myVectors[1][1] = stanVecs[1][1] * myOffsets[1];
							myVectors[1][2] = stanVecs[1][2] * myOffsets[1];
							break;
						case(150):
							myOffsets[2] --;
							myVectors[2][0] = stanVecs[2][0] * myOffsets[2];
							myVectors[2][1] = stanVecs[2][1] * myOffsets[2];
							myVectors[2][2] = stanVecs[2][2] * myOffsets[2];
							break;
						case(210):
							myOffsets[3] --;
							myVectors[3][0] = stanVecs[3][0] * myOffsets[3];
							myVectors[3][1] = stanVecs[3][1] * myOffsets[3];
							myVectors[3][2] = stanVecs[3][2] * myOffsets[3];
							break;
						case(270):
							myOffsets[4] --;
							myVectors[4][0] = stanVecs[4][0] * myOffsets[4];
							myVectors[4][1] = stanVecs[4][1] * myOffsets[4];
							myVectors[4][2] = stanVecs[4][2] * myOffsets[4];
							break;
						case(330):
							myOffsets[5] --;
							myVectors[5][0] = stanVecs[5][0] * myOffsets[5];
							myVectors[5][1] = stanVecs[5][1] * myOffsets[5];
							myVectors[5][2] = stanVecs[5][2] * myOffsets[5];
							break; 
					}
					sumVectors();
					break;
				case( Keyboard.DOWN ):
					switch( Math.abs( Math.round(_cam.rotationY)) ) {
						case(30):
							myOffsets[0] ++;
							myVectors[0][0] = stanVecs[0][0] * myOffsets[0];
							myVectors[0][1] = stanVecs[0][1] * myOffsets[0];
							myVectors[0][2] = stanVecs[0][2] * myOffsets[0];
							break;
						case(90):
							myOffsets[1] ++;
							myVectors[1][0] = stanVecs[1][0] * myOffsets[1];
							myVectors[1][1] = stanVecs[1][1] * myOffsets[1];
							myVectors[1][2] = stanVecs[1][2] * myOffsets[1];
							break;
						case(150):
							myOffsets[2] ++;
							myVectors[2][0] = stanVecs[2][0] * myOffsets[2];
							myVectors[2][1] = stanVecs[2][1] * myOffsets[2];
							myVectors[2][2] = stanVecs[2][2] * myOffsets[2];
							break;
						case(210):
							myOffsets[3] ++;
							myVectors[3][0] = stanVecs[3][0] * myOffsets[3];
							myVectors[3][1] = stanVecs[3][1] * myOffsets[3];
							myVectors[3][2] = stanVecs[3][2] * myOffsets[3];
							break;
						case(270):
							myOffsets[4] ++;
							myVectors[4][0] = stanVecs[4][0] * myOffsets[4];
							myVectors[4][1] = stanVecs[4][1] * myOffsets[4];
							myVectors[4][2] = stanVecs[4][2] * myOffsets[4];
							break;
						case(330):
							myOffsets[5] ++;
							myVectors[5][0] = stanVecs[5][0] * myOffsets[5];
							myVectors[5][1] = stanVecs[5][1] * myOffsets[5];
							myVectors[5][2] = stanVecs[5][2] * myOffsets[5];
							break; 
					}
					sumVectors();
					break;
				case ( Keyboard.RIGHT ):
					if( _cam.rotationY > 300 ) {
						_cam.rotationY = -30;
					}
					MainInput.allowInput = false;
					TweenLite.to(_cam, .5, { rotationY: _cam.rotationY + 60, onComplete: MainInput.enableInput });
					break;
				case ( Keyboard.LEFT ):
					if( _cam.rotationY < 60 ) {
						_cam.rotationY = 390;
					}
					MainInput.allowInput = false;
					TweenLite.to(_cam, .5, { rotationY: _cam.rotationY - 60, onComplete: MainInput.enableInput });
					break;
		   }
	   }
		
	}
	
}